
import { _decorator, Component, Node, Button, Prefab, instantiate } from 'cc';
import { BaseView } from './Frame/BaseView';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {
    @property(Node)
    public viewContainer: Node;

    @property(Button)
    public buttonSettleView: Button;
    @property(Button)
    public buttonItemInfoView: Button;
    @property(Button)
    public buttonDialogView: Button;
    @property(Button)
    public buttonActionQueueView: Button;

    @property(Prefab)
    public prefabViewSettle: Prefab;
    @property(Prefab)
    public prefabViewDialog: Prefab;
    @property(Prefab)
    public prefabViewItemInfo: Prefab;
    @property(Prefab)
    public prefabViewActionQueue: Prefab;

    private viewStack: BaseView[] = [];
    public static instance: Game = null;

    onLoad() {
        Game.instance = this;
        (window as any).game = this;
        this.viewStack = [];

        this.buttonSettleView.node.on(Node.EventType.TOUCH_END, () => { this.loadView("ViewSettle") });
        this.buttonDialogView.node.on(Node.EventType.TOUCH_END, () => { this.loadView("ViewDialog") });
        this.buttonItemInfoView.node.on(Node.EventType.TOUCH_END, () => { this.loadView("ViewItemInfo") });
        this.buttonActionQueueView.node.on(Node.EventType.TOUCH_END, () => { this.loadView("ViewActionQueue") });
    }

    loadView(viewName: string, callback?: Function) {
        let view = instantiate(this[`prefab${viewName}`]);
        this.viewContainer.addChild(view);
        this.viewStack.push(view.getComponent(BaseView));

        callback && callback();
    }

    onViewDestory(view: BaseView) {
        this.viewStack.pop();
    }
}